package cn.comtom.ewbs.signature.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.comtom.ewbs.signature.common.vo.response.ApiResponse;
import cn.comtom.ewbs.signature.service.impl.Sjj1507ServiceImpl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.servlet.ServletUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

//git.comtom.cn:18000/ct-ewbsv2.0/ct-ewbsv2.0.git

@Slf4j
@RestController
@RequestMapping("/sjj1507")
@Api(tags = "sjj1507签名服务")
public class Sjj1507Controller extends BaseController{

    @Autowired
    private Sjj1507ServiceImpl signatureService;

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@PostMapping(value="/trustedCertSnchainApply",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiImplicitParams({
        @ApiImplicitParam(value = "申请txt文件",name = "txtFile",required = true,allowMultiple = true,dataType = "MultipartFile")
    })
    @ApiOperation(value = "通过证书安全代理申请证书和信任列表", notes = "需要使用txt文件进行申请，每条申请一行，每行格式：path=证书列表 1.证书列表 2.证书列表 3&opt=sign&SMSN=安全模块证书号",produces=MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void trustedCertSnchainApply(@RequestParam(name = "txtFile")  MultipartFile txtFile){
        // 设定所接收的文件的名称
    	try {
			File tFile =  FileUtil.newFile("/trustedCertSnchainReq.txt");
			txtFile.transferTo(tFile);
			List<String> lines = FileUtil.readUtf8Lines(tFile);
			String fileName = "trustedCertSnchainResp.txt";
			ServletUtil.setHeader(getHttpResonse(), "Content-Disposition", "attachment; filename=".concat(fileName));
			String trustStr = signatureService.trustedCertSnchainApply(lines);
			ServletUtil.write(getHttpResonse(), new FileInputStream(new File("/trustedCertSnchainResp.txt")));
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    @ApiOperation(value = "下载申请的证书和信任列表", notes = "下载申请的证书和信任列表",produces=MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @GetMapping(value="/exportTrustedCertSnchainFile",produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ApiResponse exportDeviceSignFile() {
    	log.info("requst exportTrustedCertSnchainFile");
    	try {
    		String fileName = "trustedCertSnchainResp.txt";
			ServletUtil.setHeader(getHttpResonse(), "Content-Disposition", "attachment; filename=".concat(fileName));
			ServletUtil.write(this.getHttpResonse(), new FileInputStream("/trustedCertSnchainResp.txt"));
			FileUtil.del("/trustedCertSnchainResp.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
    	return ApiResponse.ok();
    }
}
